# -*- encoding="utf-8" -*-
# python 使用第三方SMTP發送郵件 https://www.runoob.com/python/python-email.html

import smtplib
from email.mime.text import MIMEText
from email.header import Header

def send():
    # 第三方 SMTP 服务
    mail_host="smtp.163.com" # 设置服务器
    mail_user="xd_beta" # 用户名
    mail_pass="PASSWORD" # 口令/授權碼 

    sender = 'xd_beta@163.com' # 發送郵件
    receivers = ['xd_beta@163.com'] # 接收邮件，可设置为你的QQ邮箱或者其他邮箱

    send_mail = 'This is not an email.'
    message = MIMEText(send_mail, 'plain', 'utf-8') # 內容

    # 標誌送件人和收件人，部分郵箱會過濾送件人與收件人標誌，先嘗試手動發送後觀察再編輯該標誌，其中使用與服務器不同的域名可能會被 DMARC檢查 識別為垃圾郵件
    message['From'] = Header("IiiIIiIIiI <IiiIIiIIiI@163.com>") 
    message['To'] =  Header("IiiIIiIIiI<IiiIIiIIiI@d-qq.com>")

    # 標題
    subject = 'This is not a title'
    message['Subject'] = Header(subject, 'utf-8')

    try:
        smtpObj = smtplib.SMTP() 
        smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
        smtpObj.login(mail_user,mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print('Success')
    except smtplib.SMTPException as e:
        print('Fail:', e)